package cz.byteworks.android.myway.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
import android.widget.Toast;
import cz.byteworks.android.myway.activities.DataActivity;
import cz.byteworks.android.myway.activities.R;
import cz.byteworks.android.myway.db.DataHelper;
import cz.byteworks.android.myway.location.AutoLocator;
import cz.byteworks.android.myway.location.PositionService;

public class MyWayWidget extends AppWidgetProvider {

	private RemoteViews views;
	private static final String ACTION_WIDGET_LOCATE_NOW = "ActionWidgetLocateNow";
	private static final String ACTION_WIDGET_AUTOLOCATION = "ActionWidgetAutolocation";

	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		final int N = appWidgetIds.length;
		views = new RemoteViews(context.getPackageName(), R.layout.widget);
		for (int i = 0; i < N; i++) { // pro vsechny instance widgetu
			int appWidgetId = appWidgetIds[i];
			
			// udalost prvniho tlacitka (je potreba definovat i pro skryte tlacitko) 
			Intent intentLocateNow = new Intent(context, MyWayWidget.class);
			intentLocateNow.setAction(ACTION_WIDGET_LOCATE_NOW);
			PendingIntent pendingIntentLocateNow = PendingIntent.getBroadcast(context, 0, intentLocateNow, 0);
			views.setOnClickPendingIntent(R.id.button_locate_now, pendingIntentLocateNow);
			
			Intent intentLocateNowAct = new Intent(context, MyWayWidget.class);
			intentLocateNowAct.setAction(ACTION_WIDGET_LOCATE_NOW);
			PendingIntent pendingIntentLocateNowAct = PendingIntent.getBroadcast(context, 0, intentLocateNowAct, 0);
			views.setOnClickPendingIntent(R.id.button_locate_now_act, pendingIntentLocateNowAct);
			
			// udalost druheho tlacitka (je potreba definovat i pro skryte tlacitko) 
			Intent intentAutolocation = new Intent(context, MyWayWidget.class);
			intentAutolocation.setAction(MyWayWidget.ACTION_WIDGET_AUTOLOCATION);
			PendingIntent pendingIntentAutolocation = PendingIntent.getBroadcast(context, 0, intentAutolocation, 0);
			views.setOnClickPendingIntent(R.id.button_auto_location, pendingIntentAutolocation);
			
			Intent intentAutolocationAct = new Intent(context, MyWayWidget.class);
			intentAutolocationAct.setAction(MyWayWidget.ACTION_WIDGET_AUTOLOCATION);
			PendingIntent pendingIntentAutolocationAct = PendingIntent.getBroadcast(context, 0, intentAutolocationAct, 0);
			views.setOnClickPendingIntent(R.id.button_auto_location_act, pendingIntentAutolocationAct);
			
			// udalost tretiho tlacitka - spoustime aktivitu
			Intent intentData = new Intent(context, DataActivity.class);
			intentData.setAction("android.intent.action.MAIN");
			PendingIntent pendingIntentData = PendingIntent.getActivity(context, 0, intentData, 0);
			views.setOnClickPendingIntent(R.id.button_data, pendingIntentData);

			// manualne donutime widget k aktualizaci
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(ACTION_WIDGET_LOCATE_NOW)) {
			DataHelper dh = new DataHelper(context);
			if (dh.getActualWay() == 0) {
				Toast.makeText(context, "First, create a new Way", 6000).show();
			} else {
				if (AutoLocator.isLocating()) {
					AutoLocator.stopLocating(context);
				} else {
					Toast.makeText(context, "Saving actual position", 4000).show();
					PositionService positionService = new PositionService(context, new DataHelper(context));
					positionService.storeActualPosition();
				}
			}
		}
		if (intent.getAction().equals(ACTION_WIDGET_AUTOLOCATION)) {
			DataHelper dh = new DataHelper(context);
			if (dh.getActualWay() == 0) {
				Toast.makeText(context, "First, create a new Way", 6000).show();
			} else {
				if (AutoLocator.isAutolocationRunning()) {
					Toast.makeText(context, "Tracking stopped", 4000).show();
					AutoLocator.stopAutolocation(context);					
				} else {
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
					Toast.makeText(context, "Tracking started", 4000).show();
					AutoLocator.startAutolocation(context, new DataHelper(context), new Integer(prefs.getString("prefPeriodKey", "60")));
				}
			}
		}
		super.onReceive(context, intent);
	}
}
